package com.example.demo.web;

import com.example.demo.entity.Question;
import com.example.demo.param.dto.question.AddQuestionDto;
import com.example.demo.param.dto.question.UpdateQuestionDto;
import com.example.demo.param.vo.QuestionArrVO;
import com.example.demo.param.vo.QuestionVO;
import com.example.demo.service.IQuestionService;
import com.example.demo.util.Json;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 问题
 */
@Api(tags = "问题")
@RestController
@RequestMapping("question")
public class QuestionController extends BaseController {

    @Autowired private IQuestionService questionService;

    @ApiOperation("发布问题")
    @PostMapping("add")
    public Json<Boolean> add(@RequestBody AddQuestionDto dto){
        String uid = this.verification(dto.getToken());
        Boolean exit = this.questionService.insert(uid, dto.getCategroy(), dto.getTitle(), dto.getContent());
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("删除问题")
    @DeleteMapping("delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "String")
    })
    private Json<Boolean> delete(@RequestParam String id, @RequestParam String token){
        this.verification(token);
        Boolean exit = this.questionService.delete(id);
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("修改问题")
    @PutMapping("update")
    public Json<Boolean> update(@RequestBody UpdateQuestionDto dto){
        String uid = this.verification(dto.getToken());
        Boolean exit = this.questionService.update(dto.getId(),uid,dto.getTitle(),dto.getContent(),dto.getIsStatu());
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("查询单条")
    @GetMapping("get/{id}")
    public Json<QuestionVO> get(@ApiParam(name = "id", value = "问题ID", required = true)@PathVariable("id") String id){
        return new Json<QuestionVO>(this.questionService.get(id));
    }

    @ApiOperation("模糊查询")
    @GetMapping("get/query/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category", value = "分类信息ID", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "content", value = "内容", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "uid", value = "用户id", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "statu", value = "解决状态0未解决1已解决", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "page", value = "展示的页数", required = true, paramType = "query", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示几条", required = true, paramType = "query", dataType = "int", example = "10")
    })
    public Json<QuestionArrVO> getQueryPage(
            @RequestParam(required = false) String category, @RequestParam(required = false) String title,
            @RequestParam(required = false) String content,@RequestParam(required = false) String uid,
            @RequestParam(required = false) Integer statu, @RequestParam Integer page, @RequestParam Integer pageSize){
        return new Json<QuestionArrVO>(this.questionService.getQueryPage(category, title, content, uid, statu, page, pageSize));
    }

    @ApiOperation("搜索引擎")
    @GetMapping("get/seach")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "展示的页数", required = true, paramType = "query", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示几条", required = true, paramType = "query", dataType = "int", example = "10")
    })
    public Json<QuestionArrVO> getSeach(
            @RequestParam String title, @RequestParam Integer page, @RequestParam Integer pageSize){
        return new Json<QuestionArrVO>(this.questionService.getQueryPage(null, title, null, null, null, page, pageSize));
    }

}
